#include "button.h"
#include <QDebug>
#include <QPainter>
#include "mainwindow.h"
#include <QMessageBox>
#include <QAction>

#define BUTTONWIDTH 36
#define BUTTONHEIGHT 36

bool Button::video_start = false;
bool Button::video_mode = false;
bool Button::voice_mode = false;
bool Button::picturepage_show = false;
bool Button::delay_mode = false;
Button::Button(QWidget *parent)
  : QWidget(parent)
{
    QString btnStyle="QPushButton{border-radius:4px;}";
    //辅助的功能类
    gsettingItem = new DefaultGetSet;//gsetting读取设置的接口类

    //btnLayout的界面设置相关
    pictureWrap = new QWidget;
    pictureWrap->setFixedSize(48,36);
    picture = new QLabel;
    picture->setFixedSize(48,36);
    picture->setFocusPolicy(Qt::NoFocus);
    picStackWidget = new QStackedLayout;
    picStackWidget->setStackingMode(QStackedLayout::StackAll);
//    picStackWidget->setWindowOpacity(0);
    pictureVideo = new QPushButton;

    pictureVideo->setFlat(true);
    pictureVideo->setStyleSheet("QPushButton{border:none;background:transparent;}");
    pictureVideo->setFixedSize(48,36);
    pictureVideo->setIconSize(QSize(22,22));
    pictureVideo->setIcon(QIcon(":/image/ukui-play-symbolic.svg"));
    pictureVideo->setWindowOpacity(0);
    picStackWidget->addWidget(picture);
    picStackWidget->addWidget(pictureVideo);
    pictureWrap->setLayout(picStackWidget);

    delay = new kdk::KPressButton;
    delay->setCheckable(true);
    delay->setFocusPolicy(Qt::NoFocus);
    delay->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
    cheese = new QPushButton;
    cheese->setFixedSize(50,50);
    cheese->setCheckable(true);
    cheese->setStyleSheet(btnStyle);
    cheese->setIconSize(QSize(50,50));
    cheese->setFocusPolicy(Qt::NoFocus);
    cheese_dark = new QPushButton;
    cheese_dark->hide();  //lhl debug
    cheese_dark->setFixedSize(50,50);
    cheese_dark->setCheckable(true);
    cheese_dark->setStyleSheet(btnStyle);
    cheese_dark->setIconSize(QSize(50,50));
    cheese_dark->setFocusPolicy(Qt::NoFocus);
    cheese_video = new QPushButton;
    cheese_video->setFixedSize(50,50);
    cheese_video->setStyleSheet(btnStyle);
    cheese_video->setIconSize(QSize(50,50));
    cheese_video->setFocusPolicy(Qt::NoFocus);
    cheese_stop = new QPushButton;
    cheese_stop->hide();
    cheese_stop->setFixedSize(50,50);
    cheese_stop->setStyleSheet(btnStyle);
    cheese_stop->setIconSize(QSize(50,50));
    cheese_stop->setFocusPolicy(Qt::NoFocus);
    mirror = new kdk::KPressButton;
    mirror->setCheckable(true);
    mirror->setFocusPolicy(Qt::NoFocus);
    mirror->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
    grid = new kdk::KPressButton;
    grid->setCheckable(true);
    grid->setFocusPolicy(Qt::NoFocus);
    grid->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
    burst = new kdk::KPressButton;
    burst->setCheckable(true);
    burst->setFocusPolicy(Qt::NoFocus);
    burst->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);

    btnLayout1 = new QHBoxLayout;
    btnLayout1->addStretch(1);
    btnLayout1->addWidget(mirror);
    btnLayout1->addSpacing(16);
    btnLayout1->addWidget(delay);
    btnLayout1->addSpacing(22);
    btnLayout1->addWidget(cheese);
    btnLayout1->addWidget(cheese_dark);
    btnLayout1->addSpacing(22);
    btnLayout1->addWidget(burst);
    btnLayout1->addSpacing(16);
    btnLayout1->addWidget(grid);    
    btnLayout1->addStretch(1);
    btnLayout1->addSpacing(75);
    btnLayout1->setMargin(0);
    btnWidget1 = new QWidget(this);
    btnWidget1->setLayout(btnLayout1);

    m_videoDelay = new kdk::KPressButton;
    m_videoDelay->setCheckable(true);
    m_videoDelay->setFocusPolicy(Qt::NoFocus);
    m_videoDelay->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
    m_videoDelay->setIcon(QIcon::fromTheme("ukui-countselect-symbolic"));

    m_videoGrid = new kdk::KPressButton;
    m_videoGrid->setCheckable(true);
    m_videoGrid->setFocusPolicy(Qt::NoFocus);
    m_videoGrid->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
    m_videoGrid->setIcon(QIcon::fromTheme("ukui-grid-close-symbolic"));

    btnLayout2 = new QHBoxLayout;
    btnLayout2->addStretch(1);
    btnLayout2->addWidget(m_videoDelay);
    btnLayout2->addSpacing(22);
    btnLayout2->addWidget(cheese_video);
    btnLayout2->addWidget(cheese_stop);
    btnLayout2->addSpacing(22);
    btnLayout2->addWidget(m_videoGrid);
    btnLayout2->addStretch(1);
    btnLayout2->addSpacing(72);
    btnLayout2->setMargin(0);
    btnWidget2 = new QWidget(this);
    btnWidget2->setLayout(btnLayout2);

    btnContainerLayout = new QStackedLayout;
    btnContainerLayout->addWidget(btnWidget1);
    btnContainerLayout->addWidget(btnWidget2);

    //switchLayout的界面设置相关
    capture = new kdk::KPressButton;
    capture->setFixedSize(48,24);
    capture->setCheckable(true);
    capture->setFocusPolicy(Qt::NoFocus);
    video = new kdk::KPressButton;
    video->setFixedSize(48,24);
    video->setCheckable(true);
    video->setFocusPolicy(Qt::NoFocus);
    switchLayout = new QHBoxLayout;
    switchLayout->addStretch(99);
    switchLayout->addWidget(capture);
    switchLayout->addSpacing(-8);
    switchLayout->addWidget(video);
    switchLayout->addStretch(99);
    switchLayout->setMargin(0);

    picAndButton = new QHBoxLayout;
    picAndButton->addSpacing(20);
    picAndButton->addWidget(pictureWrap);
    picAndButton->addLayout(btnContainerLayout);

    //界面总体布局
    vLayout = new QVBoxLayout;
    vLayout->addSpacing(5);
    vLayout->addLayout(switchLayout);
    vLayout->addLayout(picAndButton);
    vLayout->setMargin(0);
    this->setLayout(vLayout);
    this->setAttribute(Qt::WA_StyledBackground);


    //从配置文件中读取之前的配置
    if(set->value("model").toInt()){//gsetting记录应用上次关闭前处于拍照模式/录像模式
        btnContainerLayout->setCurrentWidget(btnWidget1);

        video_mode = false;
        capture->setChecked(1);
        video->setChecked(0);
#ifdef __V10__
        capture->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
#endif
    }
    else{
        video_mode = true;
        btnContainerLayout->setCurrentWidget(btnWidget2);
        capture->setChecked(0);
        video->setChecked(1);

#ifdef __V10__
      video->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
#endif
    }
#ifdef __V10__
//    capture->setIcon(QIcon(":/image/camera1.png"));
//    video->setIcon(QIcon(":/image/video1.png"));
    cheese->setIcon(QIcon(":/image/paizhao-kaishi.png"));
    cheese_dark->setIcon(QIcon(":/image/shot-dark.png"));
    picture->setIcon(QIcon(":/image/picture1.png"));
    cheese_video->setIcon(QIcon(":/image/luxiang-kaishi.png"));
    cheese_stop->setIcon(QIcon(":/image/luxiang-zanting.png"));
#else
    capture->setIcon(QIcon::fromTheme("ukui-photo-camera"));
    video->setIcon(QIcon::fromTheme("ukui-video-camera"));
    cheese->setIcon(QIcon(":/image/shot-click.svg"));
    cheese_dark->setIcon(QIcon(":/image/shot-dark.png"));
    cheese_video->setIcon(QIcon(":/image/actiondark-click.svg"));
    cheese_stop->setIcon(QIcon(":/image/complete.svg"));
    delay->setIcon(QIcon::fromTheme("ukui-countselect-symbolic"));
    grid->setIcon(QIcon::fromTheme("ukui-grid-close-symbolic"));
    mirror->setIcon(QIcon::fromTheme("object-flip-horizontal-symbolic"));
    burst->setIcon(QIcon::fromTheme("ukui-continuous-close-symbolic"));

    capture->setToolTip(tr("capture mode"));
    video->setToolTip(tr("video mode"));
    cheese->setToolTip(tr("cheese"));
    cheese_dark->setToolTip(tr("cheese"));
    cheese_video->setToolTip(tr("video"));
    cheese_stop->setToolTip(tr("stop"));
    pictureWrap->setToolTip(tr("album"));
    delay->setToolTip(tr("delay"));
    mirror->setToolTip(tr("mirror"));
    grid->setToolTip(tr("grid"));
    burst->setToolTip(tr("seiral shoot"));
    //适配窗管
    picture->setProperty("useIconHighlightEffect", 0x8);
    capture->setProperty("useIconHighlightEffect", 0x8);
    video->setProperty("useIconHighlightEffect", 0x8);
    cheese->setProperty("useIconHighlightEffect", 0x8);
    cheese_dark->setProperty("useIconHighlightEffect", 0x8);
    delay->setProperty("useIconHighlightEffect", 0x8);
    grid->setProperty("useIconHighlightEffect", 0x8);
    mirror->setProperty("useIconHighlightEffect", 0x8);
    burst->setProperty("useIconHighlightEffect", 0x8);
#endif
    initConnect();
    initThemeApt();

    pictureWrap->installEventFilter(this);
    pictureVideo->installEventFilter(this);
}
Button::~Button(){
    delete gsettingItem;
    delete picture;
    delete delay;
    delete cheese;
    delete cheese_dark;
    delete cheese_video;
    delete cheese_stop;
    delete mirror;
    delete grid;
    delete burst;
    delete capture;
    delete video;
}
void Button::initThemeApt(){
    //拍照按钮的图标变换
    //获取主题 lhl debug
    nowThemeStyle = gsettingItem->getStyleName();
    qDebug() << "has_device:" << HasDevicePage::has_device;
    //拍照按钮的图标变换 黑夜模式
    if(!video_mode){
        if(nowThemeStyle == "ukui-dark" || nowThemeStyle == "ukui-black"){
            cheese_dark->show();
            cheese->hide();
        }
        else{
            cheese_dark->hide();
            cheese->show();
        }
    }

    //拍照按钮的图标变换
    connect(cheese_dark,&QPushButton::pressed,this,[=](){
        if(!video_mode && HasDevicePage::has_device){
            cheese_dark->setIcon(QIcon(":/image/shot-dark-click.svg"));
        }
    });
    connect(cheese,&QPushButton::pressed,this,[=](){
        if(!video_mode && HasDevicePage::has_device){
            cheese->setIcon(QIcon(":/image/shot-hover.svg"));
        }
    });
}
void Button::initConnect(){
    connect(capture,&QPushButton::clicked,this,&Button::capture_click);
    connect(video,&QPushButton::clicked,this,&Button::video_click);
    connect(cheese,&QPushButton::clicked,this,&Button::cheese_click);
    connect(cheese_dark,&QPushButton::clicked,this,&Button::cheese_dark_click);
    connect(cheese_stop,&QPushButton::clicked,this,&Button::cheese_stop_click);
    connect(gsettingItem,&DefaultGetSet::sigThemeChange, this,&Button::dealSystemGsettingChange);
}
void Button::paintEvent(QPaintEvent* event){
    QPainter painter(this);
    QString themeNow = gsettingItem->getStyleName();
    //录像模式控制面板背景色
    QColor videoModeBtnBackground = Qt::black;
    videoModeBtnBackground.setAlpha(76);
    //拍照模式-》浅色模式控制面板背景色
    QColor capModeLightBtnBackground = Qt::white;
    //拍照模式-》深色模式控制面板背景色
    QColor videoModeDarkBtnBackground = Qt::black;
    if(video_mode){
        painter.fillRect(rect(),videoModeBtnBackground);
    }else{
        if("ukui-dark" == themeNow || "ukui-black" == themeNow){
            painter.fillRect(rect(),videoModeDarkBtnBackground);
        }else{
            painter.fillRect(rect(),capModeLightBtnBackground);
        }
    }

    QWidget::paintEvent(event);

}

void Button::capture_click(){
    //开始录像时不能切换
    nowThemeStyle = gsettingItem->getStyleName();
    if(!video_start){  
        if(nowThemeStyle == "ukui-dark" || nowThemeStyle == "ukui-black"){
            cheese_dark->show();
            cheese->hide();
        }else{
            cheese_dark->hide();
            cheese->show();
        }
        btnContainerLayout->setCurrentWidget(btnWidget1);
        video_mode = false;
        capture->setChecked(1);
        video->setChecked(0);
#ifdef __V10__
        capture->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
        video->setStyleSheet("QPushButon{background-color:#000000}");
#endif
    }else{
        video_mode = true;
        capture->setChecked(0);
        video->setChecked(1);
    }
    Q_EMIT winHeight();
}

void Button::video_click(){
    if(!video_start && !delay_mode){
        video_mode = true;
        capture->setChecked(0);
        video->setChecked(1);
#ifdef __V10__
        video->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
        capture->setStyleSheet("QPushButon{background-color:#000000}");
#endif
        btnContainerLayout->setCurrentWidget(btnWidget2);
        set->setValue("model",0);
    }
    if(video_start){
        video->setChecked(1);
    }
    if(delay_mode){
        video->setChecked(0);
    }
    if(delay->isChecked() && delay->text() == ""){
        delay->setChecked(false);
    }
    if(burst->isChecked() && burst->text() == ""){
        burst->setChecked(false);
    }
    Q_EMIT winHeight();
}

void Button::cheese_click(){
    //拍照模式
    if(!video_mode && HasDevicePage::has_device){
         cheese->setIcon(QIcon(":/image/shot-click.svg"));
      }
}

void Button::cheese_dark_click(){
    //拍照模式
    if(!video_mode && HasDevicePage::has_device){
         cheese_dark->setIcon(QIcon(":/image/shot-dark.png"));
      }
}

void Button::cheese_video_click(){
    //录像开始
    if(HasDevicePage::has_device){
             video_start = 1;
             cheese_stop->show();
             cheese_video->hide();
    }
}

void Button::cheese_stop_click(){
        video_start = 0;
        cheese_stop->hide();
        cheese_video->show();
}

void Button::voice_click(){
    //关闭录音设备
    if(!video_start){
        if(voice_mode){
              voice->setIcon(QIcon(":/image/maike-off .svg"));
              voice_mode = false;
          }
        //打开录音设备
        else{
            voice->setIcon(QIcon(":/image/maike-on .svg"));
            voice_mode = true;
        }
    }
}
void Button::stop_click(){
    if(HasDevicePage::has_device && (video_start)){
    }
}
void Button::restore_click(){
}

void Button::picture_click(){
    if(!picturepage_show){
        picturepage_show = true;
#ifdef __V10__
        picture->setStyleSheet("QPushButton{background-color:#3DBE5;border-radius:4px;}");
#endif
    }
    else{
        picturepage_show = false;
#ifdef __V10__
        picture->setStyleSheet("QPushButton{background-color:#000000;}");
#endif
    }
}

//冗余代码 需优化
void Button::dealSystemGsettingChange(const QString themeNow){
    if(!video_mode){
        if(themeNow == "ukui-dark" || themeNow == "ukui-black"){
            cheese_dark->show();
            cheese->hide();
        }
        else{
            cheese->show();
            cheese_dark->hide();
        }
    }
}

bool Button::eventFilter(QObject *watched, QEvent *event){
    if(event->type() == QEvent::MouseButtonDblClick){
        return true;
    }
    if(watched == pictureWrap || watched == pictureVideo){
        if(event->type() == QEvent::MouseButtonPress){
            if(static_cast<QMouseEvent *>(event)->button() == Qt::LeftButton){
                gsettingItem->addThumbnailSDKPoint();
                QString fileName = gsettingItem->getFileName();
                QFileInfo info(fileName);
                if(info.exists()){
                    openFile();
                }
                else{
                     openDir();
                }
            }
        }
    }
    return QWidget::eventFilter(watched,event);
}
void Button::openFile(){
    QString fileName = gsettingItem->getFileName();
    qDebug() << "latest filename" << fileName;
    QFileInfo info(fileName);
    if(info.exists()){
        QProcess proc;
        proc.startDetached("xdg-open " + fileName);
    }
}
void Button::openDir(){
    QString fileName = gsettingItem->getFileName();
    QString savePath;
    savePath = gsettingItem->getSavePath();
    if(!QDir(savePath).exists()){//路径不存在
        qDebug() << "存储路径不存在" << gsettingItem->getSavePath();
        QMessageBox mes;
        mes.warning(this,tr("waring"),tr("path does not exist, please set storage path"));
        mes.move(this->geometry().center()-mes.rect().center());
    }
    else{
        QProcess proc;
        proc.startDetached("xdg-open " + savePath);
    }
}
void Button::albumRightMenu()
{
    QMenu m_rightMenu;
    //定义action
    QAction openFilea(tr("open File"));
    QAction openDira(tr("open Dir"));
    //action绑定槽函数
    connect(&openFilea, &QAction::triggered, this, &Button::openFile);
    connect(&openDira, &QAction::triggered, this, &Button::openDir);
    //action添加到menu
    m_rightMenu.addAction(&openFilea);
    m_rightMenu.addAction(&openDira);
    //action添加到menu end
    m_rightMenu.exec(QCursor::pos());
}
